﻿using lingopen.Exceptions;
using lingopen.IdStrategy;
using lingopen.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Chloe;
using lingopen.AutoMapper;
using lingopen.Entity.CRM;
using System.Net;
using Newtonsoft.Json.Linq;
using lingopen.Application.CRM.Models.WorkBill;

namespace lingopen.Application.CRM.Implements
{
    public class CustomersAreaAppService : AdminAppService, ICustomerAreaAppService
    {
        public PagedData<CRM_Customer_Area> GetPageArea(Pagination pagination, string cust_id, string keyword)
        {
            var q = DbContext.Query<CRM_Customer_Area>().FilterDeleted();
            q = q.WhereIfNotNull(keyword, a => a.AreaName.Contains(keyword) || a.Description.Contains(keyword));
            q = q.WhereIfNotNull(cust_id, a => a.CustomerId == cust_id);
            q = q.OrderBy(p=>p.SortKey);
            PagedData<CRM_Customer_Area> ret = q.TakePageData(pagination);
            return ret;
        }
        public PagedData<CRM_Customer_AreaMap> GetPageMap(Pagination pagination, string customerid, string areaName)
        {
            var q = DbContext.Query<CRM_Customer_AreaMap>().FilterDeleted(); 
            q = q.Where(p =>p.CustomerId==customerid&& p.AreaName== areaName);
            q = q.OrderBy(p => p.Build).OrderBy(p=>p.Floor);
            PagedData<CRM_Customer_AreaMap> ret = q.TakePageData(pagination);
            return ret;
        }

        public void Add(AddAreaInput input)
        {
            input.Validate();
            this.DbContext.InsertFromDto<CRM_Customer_Area, AddAreaInput>(input);
        }

        public void Update(UpdateAreaInput input)
        {
            input.Validate();
            this.DbContext.UpdateFromDto<CRM_Customer_Area, UpdateAreaInput>(input);
        }
        public void DeleteById(string id)
        {
            id.NotNullOrEmpty();
            this.DbContext.DeleteByKey<CRM_Customer_Area>(id);
        }
    }
}
